﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.Domain;
using System.Web.Security;
using MShop.Core.DataAccess;
using MShop.Core.Service.Membership;


namespace MShop.Web.Areas.Admin.Controllers
{
    public class RolesController : SecureController
    {
        //
        // GET: /Roles/
        private readonly ICommonDao<aspnet_Roles> _CommonDao;

        public RolesController(ICommonDao<aspnet_Roles> commonDao)
        {
            _CommonDao = commonDao;
           
        }

        public ActionResult Index(Guid? id)
        {

            var list = _CommonDao.GetAll();
            ViewData["roles"] = list;
            if (Request.QueryString.Count > 0)
            {
                ModelState.AddModelError("RoleName", "角色名称不能为空");
            }
            if (id.HasValue)
            {
                return View(_CommonDao.Find(x => x.RoleId == id).First());
            }
            else
            {
                return View(new aspnet_Roles());
            }
           
        }


      

        [HttpPost]
        public ActionResult Edit(Guid id, aspnet_Roles role, FormCollection collection)
        {
            var role2 = _CommonDao.Find(x => x.RoleId == id).First();
            role2.RoleName = role.RoleName;
            role2.LoweredRoleName = role2.RoleName.ToLowerInvariant();
            role2.IsPower = role.IsPower;
            if (role.IsPower)
            {
                role2.Description = SysRole.PowerDescription;
                _CommonDao.SaveChanges();
            }
            else
            {
                role2.Description = null;
                _CommonDao.SaveChanges();
            }
            return RedirectToAction("Index");
        }       


        [HttpPost]
        public ActionResult Create(aspnet_Roles role, FormCollection collection)
        {
            if (string.IsNullOrEmpty(role.RoleName))
            {

                return RedirectToAction("Index", new { q = true });
            }
            else
            {
               
                Roles.CreateRole(role.RoleName);
                aspnet_Roles dbrole = _CommonDao.FirstOrDefault(x => x.RoleName == role.RoleName);
                if (role.IsPower)
                {                   
                    dbrole.Description = SysRole.PowerDescription;
                    _CommonDao.SaveChanges();
                }
                else
                {
                    dbrole.Description = null;
                    _CommonDao.SaveChanges();
                }
                return RedirectToAction("Index");
            }
        }

        public ActionResult Delete(Guid id)
        {
            aspnet_Roles role = _CommonDao.Find(x => x.RoleId == id).First();
            Roles.DeleteRole(role.RoleName, true);
            return RedirectToAction("Index");

        }
    }
}
